/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import me.zhengjie.business.domain.FixedCostsBatch;
import me.zhengjie.business.domain.FixedCostsMaster;
import me.zhengjie.business.repository.FixedCostsMasterRepository;
import me.zhengjie.business.service.FixedCostsMasterService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.FixedCostsBatchRepository;
import me.zhengjie.business.service.FixedCostsBatchService;
import me.zhengjie.business.service.dto.FixedCostsBatchDto;
import me.zhengjie.business.service.dto.FixedCostsBatchQueryCriteria;
import me.zhengjie.business.service.mapstruct.FixedCostsBatchMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2023-09-13
**/
@Service
@RequiredArgsConstructor
public class FixedCostsBatchServiceImpl implements FixedCostsBatchService {

    private final FixedCostsBatchRepository fixedCostsBatchRepository;
    private final FixedCostsBatchMapper fixedCostsBatchMapper;

    private final FixedCostsMasterService fixedCostsMasterService;

    @Override
    public Map<String,Object> queryAll(FixedCostsBatchQueryCriteria criteria, Pageable pageable){
        Page<FixedCostsBatch> page = fixedCostsBatchRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fixedCostsBatchMapper::toDto));
    }

    @Override
    public List<FixedCostsBatchDto> queryAll(FixedCostsBatchQueryCriteria criteria){
        return fixedCostsBatchMapper.toDto(fixedCostsBatchRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FixedCostsBatchDto findById(String bisFixedCostsId) {
        FixedCostsBatch fixedCostsBatch = fixedCostsBatchRepository.findById(bisFixedCostsId).orElseGet(FixedCostsBatch::new);
        ValidationUtil.isNull(fixedCostsBatch.getBisFixedCostsId(),"FixedCostsBatch","bisFixedCostsId",bisFixedCostsId);
        return fixedCostsBatchMapper.toDto(fixedCostsBatch);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FixedCostsBatchDto create(FixedCostsBatch resources) {

        BigDecimal djBigDecimal = resources.getBisFixedCostsDj();
        BigDecimal slBigDecimal = new BigDecimal(resources.getBisFixedCostsSl());
        BigDecimal zjBigDecimal = djBigDecimal.multiply(slBigDecimal);

        resources.setBisFixedCostsId(IdUtil.simpleUUID());
        resources.setBisFixedCostsZj(zjBigDecimal);
        FixedCostsBatchDto fixedCostsBatchDto = fixedCostsBatchMapper.toDto(fixedCostsBatchRepository.save(resources));

        String masterId = fixedCostsBatchDto.getBisFixedCostsMasterId();
        BigDecimal zj = fixedCostsBatchRepository.queryCostZj(masterId);
        FixedCostsMaster fixedCostsMaster = new FixedCostsMaster();
        fixedCostsMaster.setBisFixedCostsMasterId(masterId);
        fixedCostsMaster.setBisFixedCostsMasterAmount(zj);
        fixedCostsMasterService.update(fixedCostsMaster);

        return fixedCostsBatchDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FixedCostsBatch resources) {
        FixedCostsBatch fixedCostsBatch = fixedCostsBatchRepository.findById(resources.getBisFixedCostsId()).orElseGet(FixedCostsBatch::new);
        ValidationUtil.isNull( fixedCostsBatch.getBisFixedCostsId(),"FixedCostsBatch","id",resources.getBisFixedCostsId());

        BigDecimal djBigDecimal = resources.getBisFixedCostsDj();
        BigDecimal slBigDecimal = new BigDecimal(resources.getBisFixedCostsSl());
        BigDecimal zjBigDecimal = djBigDecimal.multiply(slBigDecimal);

        resources.setBisFixedCostsZj(zjBigDecimal);

        fixedCostsBatch.copy(resources);
        fixedCostsBatchRepository.save(fixedCostsBatch);

        String masterId = fixedCostsBatch.getBisFixedCostsMasterId();
        BigDecimal zj = fixedCostsBatchRepository.queryCostZj(masterId);
        FixedCostsMaster fixedCostsMaster = new FixedCostsMaster();
        fixedCostsMaster.setBisFixedCostsMasterId(masterId);
        fixedCostsMaster.setBisFixedCostsMasterAmount(zj);
        fixedCostsMasterService.update(fixedCostsMaster);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String bisFixedCostsId : ids) {
            fixedCostsBatchRepository.deleteById(bisFixedCostsId);
        }
    }

    @Override
    public void download(List<FixedCostsBatchDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FixedCostsBatchDto fixedCostsBatch : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}